/*
 * TextHighlighter.java  0.4.0 / Oct 25, 2010
 *
 * Copyright (c) 2010 - 2011, cute4j.com and contributors.
 *
 * This software is licensed under the LGPL version 2.1 license.
 * A copy of this license is included in the file LICENSE-LGPL.txt.
 */
package com.cute4j.swing.text;

/**
 * Abstract Text Highlighter.
 *
 * @author Slobodan Vrkacevic
 * @version 0.4.0
 */
public abstract class TextHighlighter {

// Private Fields. -------------------------------------------------------------

  private final Object lock;
  private TextDocument currentDocument;

// Public Constructors. --------------------------------------------------------

  /**
   * Constructs new TextHighlighter object.
   */
  public TextHighlighter() {
    lock = new Object();
  }

// Protected Methods. ----------------------------------------------------------

  /**
   * Highlighs specified block of text.
   *
   * @param blockStart the start of the block
   * @param blockText the text of the block
   */
  protected abstract void highlightBlock(int blockStart, String blockText);

  /**
   * Sets the color for the specified text segment.
   *
   * @param start the start of the segment
   * @param length the length of the segment
   * @param format the character format
   */
  protected void setFormat(int start, int length, CharacterFormat format) {
    synchronized (lock) {
      if (currentDocument != null) {
        currentDocument.setFormat(start, length, format);
      }
    }
  }

// Package-Private Methods. ----------------------------------------------------

  void highlightDocumentBlock(TextDocument document, int blockStart,
                              String blockText) {
    synchronized (lock) {
      currentDocument = document;
      highlightBlock(blockStart, blockText);
      currentDocument = null;
    }
  }

}
